import Router from 'koa-router';
import {files} from '../db/models.js'
import {messageQueue} from '../utils/messageQueue.js'

const get_knowledge_base_files = new Router();

get_knowledge_base_files.get('/', async (ctx) => {
    try{
        const { knowledge_base } = ctx.request.query;
        const documents = await files.find({ knowledgeBase: knowledge_base}).select('-_id -__v -path -ids')
        for(const file of messageQueue){
            if(file.status === 'embedding' && file.knowledgeBase === knowledge_base){
                documents[documents.findIndex(doc => doc.name === file.name)].status = 'embedding'
                break;
            }
        }
        ctx.body = {
            code: 200,
            data: documents.reverse()
        };
    }catch (e){
        ctx.status = 500
        ctx.body = {
            code: 500,
            message: '服务器内部错误'
        }
        console.log(e.name + "：" + e.message)
    }
});
export default get_knowledge_base_files;
